package bp.tools;

import bp.da.DataType;
import bp.da.Log;
import bp.difference.ContextHolderUtils;
import net.sf.json.JSONObject;

public class SystemUtil {
    /**
     * 根据ip获取地址
     * http://whois.pconline.com.cn:
     * @return {"ip":"123.232.211.159","pro":"山东省","proCode":"370000"
    ,"city":"济南市","cityCode":"370100","region":"","regionCode":"0","addr":"山东省济南市 联通","regionNames":"","err":""}
     *   http://ip-api.com/json/:
     *   {"status":"success","country":"中国","countryCode":"CN","region":"SD","regionName":"山东","city":"青岛市"
     *   ,"lat":36.0662,"lon":120.383,"timezone":"Asia/Shanghai","isp":"China Unicom Shandong Province Network","org":"","as":"AS4837 CHINA UNICOM China169 Backbone"}
     */
    public static String GetCityByIp() {
           String ip = ContextHolderUtils.getRequest().getHeader("X-Forwarded-For");
            Log.DebugWriteInfo("请求头：[X-Forwarded-For]=" + ip);
            if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
                ip = ContextHolderUtils.getRequest().getRemoteAddr();
            }
            Log.DebugWriteInfo("远程地址(RemoteAddr)=" + ip);
            if("127.0.0.1".equals(ip) || "::1".equals(ip)){
                try {
                    String rstr = HttpClientUtil.doGet("http://httpbin.org/ip");
                    JSONObject json =JSONObject.fromObject(rstr);
                    ip = (String) json.get("origin");
                    Log.DebugWriteInfo("查询本地源地址(origin)=" + ip);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
//        String urlString = "http://ip-api.com/json/" + ip + "?fields=status,message,country,countryCode,region,regionName,city,lat,lon,timezone,isp,org,as&lang=zh-CN";
		String urlString = "http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true";
        Log.DebugWriteInfo("查询归属地：" + urlString);
        return HttpClientUtil.sendGet(urlString,null,"GBK");
    }

    //获取客户端系统
    public static String GetOperatingSystem()
    {
        // 获取 User-Agent 头部
        String userAgent = ContextHolderUtils.getRequest().getHeader("User-Agent").toString();
        if (userAgent.contains("Windows"))
            return "Windows";
        else if (userAgent.contains("Macintosh"))
            return "Mac OS";
        else if (userAgent.contains("Linux"))
            return "Linux";
        else if (userAgent.contains("Android"))
            return "Android";
        else if (userAgent.contains("iPhone"))
            return "iOS (iPhone)";
        else if (userAgent.contains("iPad"))
            return "iOS (iPad)";
        else
            return "未知操作系统";
    }
    //获取客户端浏览器
    public static String GetBrowser()
    {
        // 获取 User-Agent 头部
        String userAgent = ContextHolderUtils.getRequest().getHeader("User-Agent").toString();
        if (userAgent.contains("Chrome"))
            return "Chrome";
        else if (userAgent.contains("Firefox"))
            return "Firefox";
        else if (userAgent.contains("Safari") && !userAgent.contains("Chrome"))
            return "Safari";
        else if (userAgent.contains("MSIE") || userAgent.contains("Trident"))
            return "Internet Explorer";
        else if (userAgent.contains("Edge"))
            return "Edge";
        else
            return "未知浏览器";
    }
}
